﻿#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
//棋盘初始化
void board_set(char arr[ROWS][COLS], int rows, int cols, char set)
{
	int i = 0;
	for (i = 0; i < rows; i++)
	{
		int j = 0;
		for (j = 0; j < cols; j++)
		{
			arr[i][j] = set;
		}
	}
}
//打印棋盘
void board_print(char arr[ROW][COL], int row, int col)
{
	int i = 0;
	for (i = 0; i <= col; i++)
	{
		printf("%d ", i);
	}
	printf("\n");
	for (i = 0; i < row; i++)
	{
		printf("%d ", i);
		int j = 0;
		for (j = 0; j < col; j++)
		{
			printf("%c ", arr[i][j]);
		}
		printf("\n");
	}
}
//布置雷
void mine_set(char arr[ROW][ROW], int row, int col)
{
	int count = 10;
	while (count)
	{
		int x = rand() % row + 1;
		int y = rand() % col + 1;
		if (arr[x][y] == '0')
		{
			arr[x][y] = '1';
			count--;
		}
	}
}
//统计周围雷
int GetMineCount(char mine[ROWS][COLS], int x, int y)
{
	return (mine[x - 1][y] + mine[x - 1][y - 1] + mine[x][y - 1] + mine[x + 1][y - 1] + 
		mine[x + 1][y + 1] + mine[x][y + 1] + mine[x - 1][y + 1] - 8 * '0');
}
//扫雷
void mine_sweep(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	int x = 0;
	int y = 0;
	int win = 0;
	while (win < row * col - 10)
	{
		printf("请输入要排查的坐标:>");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (mine[x][y] == '1')
			{
				printf("很遗憾，你被炸死了\n");
				board_print(mine, ROW, COL);
				break;
			}
			else {
				int count = GetMineCount(mine, x, y);
				show[x][y] = count + '0';
				board_print(show, ROW, COL);
				win++;
			}
		}
		else
		{
			printf("请重新输入坐标");
		}
	}
	if (win == row * col -10)
	{
		printf("恭喜你，排雷成功\n");
		board_print(mine, ROW, COL);

	}
}